/**
 * 实际管理员的一个包裹，实际的业务操作在这个类里，实体数据在SystemAdministrator类里面
 * 重构后弃用
 */
package dms.model.role.wrapper;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import dms.dto.DormSupervisorDTO;
import dms.dto.StudentAffairTeacherDTO;
import dms.dto.SystemAdministratorDTO;
import dms.model.Building;
import dms.model.School;
import dms.model.exception.NoPermissionException;
import dms.model.exception.NoSuchEntityException;
import dms.model.role.Account;
import dms.model.role.DormSupervisor;
import dms.model.role.Role;
import dms.model.role.StudentAffairTeacher;
import dms.model.role.SystemAdministrator;
import dms.repository.BuildingRepository;
import dms.repository.SchoolRepository;
import dms.repository.SystemAdministratorRepository;
import dms.util.PasswordUtil;

/**
 * @author 李健
 *
 */
public class SystemAdministratorWrapper {

	private SystemAdministrator systemAdministrator = null;
	
	//系统管理员要用到的仓储
	private static final Log log = LogFactory.getLog(SystemAdministratorWrapper.class);
	private SystemAdministratorRepository systemAdministratorRepository = null;
	private SchoolRepository schoolRepository = null;
	private BuildingRepository buildingRepository = null;
	
	/*******************************************************************************************/
	// Domain operation 
	
	public void addStudentAffairTeacher(StudentAffairTeacherDTO teacher) {
		StudentAffairTeacher studentAffairTeacher = new StudentAffairTeacher();
		Account account = new Account();
		account.setRole(Role.STUDENT_AFFAIR_TEACHER);
		
		School school = this.schoolRepository.findSchoolById(teacher.getSchool());
		try {
			BeanUtils.copyProperties(account, teacher);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		//确保account 和studentAffairTeacher 的 Id为空才能正确插入
		Date date = new Date();
		account.setRegisterDate(date);
		account.setModifyDate(date);
		account.setId(null);
		//加密 密码
		account.setPassword(PasswordUtil.MD5Digest(teacher.getPassword()));
		studentAffairTeacher.setId(null);
		studentAffairTeacher.setSchool(school);
		studentAffairTeacher.setAccount(account);
		
		this.systemAdministratorRepository.createStduentAffairTeacher(studentAffairTeacher);
	}
	
	public void updateStudentAffairTeacher(StudentAffairTeacherDTO teacher) throws NoSuchEntityException {
		StudentAffairTeacher studentAffairTeacher = this.systemAdministratorRepository.findStudentAffairTeacherById(teacher.getId());
		
		if (studentAffairTeacher == null) {
			throw new NoSuchEntityException("没有该实体");
		}
		Account account = studentAffairTeacher.getAccount();
		
		School school = this.schoolRepository.findSchoolById(teacher.getSchool());
		
		if (school == null) {
			throw new NoSuchEntityException("没有该实体");
		}
		
		//确保account 和studentAffairTeacher 有一样的 Id 才能正确修改 一对一主键映射
		account.setId(teacher.getId());
		
		//记住要修改modifyDate
		account.setModifyDate(new Date());
		if (teacher.getPassword() != null || teacher.getPassword().length() > 0) {
			//加密 密码
			account.setPassword(PasswordUtil.MD5Digest(teacher.getPassword()));
		}
		account.setEmail(teacher.getEmail());
		account.setTelephone(teacher.getTelephone());
		studentAffairTeacher.setSchool(school);
		studentAffairTeacher.setAccount(account);
		
		this.systemAdministratorRepository.updateStduentAffairTeacher(studentAffairTeacher);
	}
	
	public void deleteStudentAffairTeacherById(Long id) {
		this.systemAdministratorRepository.deleteStudentAffairTeacherById(id);
	}
	
	public void addDormSupervisor(DormSupervisorDTO dormSupervisorInfo) {
		DormSupervisor dormSupervisor = new DormSupervisor();
		Building building = this.buildingRepository.findById(dormSupervisorInfo.getBuilding());
		
		Account account = new Account(Role.DORM_SUPERVISOR);
		
		try {
			BeanUtils.copyProperties(account, dormSupervisorInfo);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
		}
		
		//确保account 和studentAffairTeacher 的 Id为空才能正确插入
		Date date = new Date();
		account.setRegisterDate(date);
		account.setModifyDate(date);
		account.setId(null);
		//加密 密码
		account.setPassword(PasswordUtil.MD5Digest(dormSupervisorInfo.getPassword()));
		dormSupervisor.setId(null);
		dormSupervisor.setAccount(account);
		dormSupervisor.setBuilding(building);
		this.systemAdministratorRepository.createDormSupervisor(dormSupervisor);
	}
	
	public void updateDormSupervisor(DormSupervisorDTO dormSupervisorInfo) throws NoSuchEntityException {
		DormSupervisor dormSupervisor = this.systemAdministratorRepository.findDormSupervisorById(dormSupervisorInfo.getId());
		
		if (dormSupervisor == null) {
			throw new NoSuchEntityException("没有该实体");
		}
		Building building = this.buildingRepository.findById(dormSupervisorInfo.getBuilding());
		
		if (building == null) {
			throw new NoSuchEntityException("没有该实体");
		}
		Account account = dormSupervisor.getAccount();
		
		account.setEmail(dormSupervisorInfo.getEmail());
		account.setTelephone(dormSupervisorInfo.getTelephone());
		
		if (dormSupervisorInfo.getPassword() != null || dormSupervisorInfo.getPassword().length() > 0) {
			account.setPassword(PasswordUtil.MD5Digest(dormSupervisorInfo
					.getPassword()));
		}
		Date date = new Date();
		account.setModifyDate(date);
		//加密 密码
		
		dormSupervisor.setBuilding(building);
		this.systemAdministratorRepository.updateDormSupervisor(dormSupervisor);
	}
	
	public void deleteDormSupervisorById(Long id) {
		this.systemAdministratorRepository.deleteDormSupervisorById(id);
		
	}
	
	public void addSystemAdministrator(SystemAdministratorDTO systemAdministratorInfo) {
		SystemAdministrator systemAdministrator = new SystemAdministrator();
		Account account = new Account(Role.SYSTEM_ADMINISTRATOR);
		
		try {
			BeanUtils.copyProperties(account, systemAdministratorInfo);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
		}
		
		account.setRole(Role.SYSTEM_ADMINISTRATOR);
		account.setRegisterDate(new Date());
		account.setModifyDate(new Date());
		account.setId(null);
		//加密 密码
		account.setPassword(PasswordUtil.MD5Digest(systemAdministratorInfo.getPassword()));
		systemAdministrator.setAccount(account);
		
		this.systemAdministratorRepository.createSystemAdministrator(systemAdministrator);
	}
	
	public void modifyPersonalInfo(SystemAdministratorDTO systemAdministratorInfo) throws NoPermissionException, NoSuchEntityException {
		SystemAdministrator sa = this.systemAdministratorRepository.findSystemAdministratorById(systemAdministratorInfo.getId());
		
		if (sa == null) {
			throw new NoSuchEntityException("没有该用户");
		}
		if (!sa.getAccount().getId().equals(systemAdministratorInfo.getId())) {
			throw new NoPermissionException("你不能修改别人的账户休息");
		}
		
		if (systemAdministratorInfo.getOldEmail() != null && systemAdministratorInfo.getEmail() != null) {
			if (sa.getAccount().getEmail().equals(
					systemAdministratorInfo.getOldEmail())) {
				sa.getAccount().setEmail(systemAdministratorInfo.getEmail());

			}
		}
		
		if (systemAdministratorInfo.getOldPassword() != null && systemAdministratorInfo.getPassword() != null) {
			if (sa.getAccount().getPassword().equals(PasswordUtil.MD5Digest(systemAdministratorInfo.getOldPassword()))) {
				sa.getAccount().setPassword(PasswordUtil.MD5Digest(systemAdministratorInfo.getPassword()));
			}
		}
		
		if (systemAdministratorInfo.getTelephone() != null) {
			sa.getAccount().setTelephone(systemAdministratorInfo.getTelephone());
		}
		
		sa.getAccount().setModifyDate(new Date());
		this.systemAdministratorRepository.updateSystemAdministrator(sa);
	}
	
	public void changeDormSupervisorPassword(DormSupervisor dormSupervisor,									
									  String newPassword) {
		dormSupervisor.getAccount().setPassword(PasswordUtil.MD5Digest(newPassword));
		dormSupervisor.getAccount().setModifyDate(new Date());
		this.systemAdministratorRepository.updateDormSupervisor(dormSupervisor);
	}
	
	public void changeDormSupervisorPassword(long dormSupervisorId,
			String newPassword) throws Exception {
		
		DormSupervisor dormSupervisor = this.systemAdministratorRepository.findDormSupervisorById(dormSupervisorId);
		
		if (dormSupervisor == null) {
			throw new NoSuchEntityException("没有该管理员");
		}
		dormSupervisor.getAccount().setPassword(PasswordUtil.MD5Digest(newPassword));
		dormSupervisor.getAccount().setModifyDate(new Date());
		this.systemAdministratorRepository.updateDormSupervisor(dormSupervisor);
	}
	
	public void changeStudentAffairTeacherPassword(StudentAffairTeacher studentAffairTeacher,											
											String newPassword) {
		studentAffairTeacher.getAccount().setPassword(PasswordUtil.MD5Digest(newPassword));
		studentAffairTeacher.getAccount().setModifyDate(new Date());
		this.systemAdministratorRepository.updateStduentAffairTeacher(studentAffairTeacher);
	}
	public void changeStudentAffairTeacherPassword(
			long studentAffairTeacherId, String newPassword) throws Exception {
		
		StudentAffairTeacher studentAffairTeacher = this.systemAdministratorRepository.findStudentAffairTeacherById(studentAffairTeacherId);
		if (studentAffairTeacher == null) {
			throw new NoSuchEntityException("没有该管理员");
		}
		studentAffairTeacher.getAccount().setPassword(PasswordUtil.MD5Digest(newPassword));
		studentAffairTeacher.getAccount().setModifyDate(new Date());

		this.systemAdministratorRepository
				.updateStduentAffairTeacher(studentAffairTeacher);
	}
	/**
	 * @return the systemAdministrator
	 */
	public SystemAdministrator getSystemAdministrator() {
		return systemAdministrator;
	}

	/**
	 * @param systemAdministrator the systemAdministrator to set
	 */
	public void setSystemAdministrator(SystemAdministrator systemAdministrator) {
		this.systemAdministrator = systemAdministrator;
	}

	/**
	 * @return the systemAdministratorRepository
	 */
	public SystemAdministratorRepository getSystemAdministratorRepository() {
		return systemAdministratorRepository;
	}

	/**
	 * @param systemAdministratorRepository the systemAdministratorRepository to set
	 */
	public void setSystemAdministratorRepository(
			SystemAdministratorRepository systemAdministratorRepository) {
		this.systemAdministratorRepository = systemAdministratorRepository;
	}

	/**
	 * @return the schoolRepository
	 */
	public SchoolRepository getSchoolRepository() {
		return schoolRepository;
	}

	/**
	 * @param schoolRepository the schoolRepository to set
	 */
	public void setSchoolRepository(SchoolRepository schoolRepository) {
		this.schoolRepository = schoolRepository;
	}

	/**
	 * @return the buildingRepository
	 */
	public BuildingRepository getBuildingRepository() {
		return buildingRepository;
	}

	/**
	 * @param buildingRepository the buildingRepository to set
	 */
	public void setBuildingRepository(BuildingRepository buildingRepository) {
		this.buildingRepository = buildingRepository;
	}

	/**
	 * @param systemAdministratorRepository
	 * @param schoolRepository
	 * @param buildingRepository
	 */
	public SystemAdministratorWrapper(
			SystemAdministratorRepository systemAdministratorRepository,
			SchoolRepository schoolRepository,
			BuildingRepository buildingRepository) {
		super();
		this.systemAdministratorRepository = systemAdministratorRepository;
		this.schoolRepository = schoolRepository;
		this.buildingRepository = buildingRepository;
	}

	/**
	 * 
	 */
	public SystemAdministratorWrapper() {
		super();
	}

	
}
